<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      canvas {
        border: 1px solid #ccc;
        margin-right:5px;
      }
    </style>
  </head>
  <body>
    <script>
      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext('2d');
       
        const img = new Image();
        img.src = '../imgs/01.png';
        img.onload = function(){
          ctx.drawImage(img,0,0,400,400);


          const imageData = ctx.getImageData(0,0,400,400);
          for(let i = 0;i<imageData.data.length;i+=4){
              const r = imageData.data[i];
              const g = imageData.data[i+1];
              const b = imageData.data[i+2];
              const a = imageData.data[i+3];

              const avg = (r+g+b)/3 ;

              imageData.data[i] = avg ;
              imageData.data[i+1] = avg ;
              imageData.data[i+2] = avg ;

          }
          ctx.putImageData(imageData,0,0);
        
          console.log(img.width);
          console.log(img.height);
        }
      })();

      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext('2d');
       
        const img = new Image();
        img.crossOrigin = 'anonymous' ;
        img.src = '../imgs/01.png';
        img.onload = function(){
          ctx.drawImage(img,0,0);


          const imageData = ctx.getImageData(0,0,400,400);
          for(let i = 0;i<imageData.data.length;i+=4){
              const r = imageData.data[i];
              const g = imageData.data[i+1];
              const b = imageData.data[i+2];
              const a = imageData.data[i+3];

              const avg = (r+g+b)/3 ;

              imageData.data[i] = 255-r ;
              imageData.data[i+1] = 255-g ;
              imageData.data[i+2] = 255-b ;

          }
          ctx.putImageData(imageData,0,0);

        }

      })();


      (() => {
        const canvas = document.createElement("canvas");
        canvas.width = 400;
        canvas.height = 400;
        document.body.append(canvas);

        const ctx = canvas.getContext('2d');
       
        const img = new Image();
        img.src = '../imgs/01.png';
        img.onload = function(){
          ctx.drawImage(img,0,0);


          const imageData = ctx.getImageData(0,0,400,400);
          for(let i = 0;i<imageData.data.length;i+=4){
              const r = imageData.data[i];
              const g = imageData.data[i+1];
              const b = imageData.data[i+2];
              const a = imageData.data[i+3];

              if(r - g > 30){

                imageData.data[i] = g ;
                imageData.data[i+1] = r ;
              }

          }
          ctx.putImageData(imageData,0,0);

        }

      })();

      
    </script>
  </body>
</html>
